package at.guigu.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

public class ServletConfiguration extends AbstractAnnotationConfigDispatcherServletInitializer {
    // 加载SpringMVC的配置
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfiguration.class};
    }
    // 设置哪些请求归SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    // 加载非SringMVC的配置，比如：加载Spring的配置
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfiguration.class};
    }

    // POST请求乱码处理：指定字符过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}

/* 等同于
// 定义一个Servlet容器启动的配置类，来加载Spring的配置
public class ServletConfiguration extends AbstractDispatcherServletInitializer {
    // 加载SpringMVC的配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(SpringMvcConfiguration.class);
        return context;
    }

    // 设置哪些请求归SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        // 代表将所有请求都交给前端控制器处理
        return new String[]{"/"};
    }

    // 加载非SringMVC的配置，比如：加载Spring的配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(SpringConfiguration.class);
        return context;
    }
    // POST请求乱码处理：指定字符过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}*/